<?php
declare (strict_types=1);

namespace app\controller\home;

use app\common\controller\BaseController;
use app\model\home\Book as bookModel;
use think\App;
use \Throwable;

class Book extends BaseController
{
    protected $model;

    public function __construct(App $app, bookModel $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    /**
     * 列表查询
     * @return array
     */
    public function query()
    {
        $where = [];
        $page = input('page/d', 1);
        $pageSize = input('page_size/d', 20);
        $order = null;
        $fields = '*';
        return $this->model->getList($where, $fields, $order, $page, $pageSize);
    }

    /**
     * @return bookModel|array|\think\Model|null
     * 详情
     */
    public function detail()
    {
        try {
            validate(\app\validate\home\Book::class)
                ->scene('detail')
                ->check($this->params);
            $this->params['member_id'] = $this->memberId;
            return $this->model->getOne($this->params);
        } catch (\Exception $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     * 新建
     * POST
     * @return array
     */
    public function create()
    {
        try {
            validate(\app\validate\home\Book::class)
                ->scene('create')
                ->check($this->params);
            $this->params['member_id'] = $this->memberId;
            return $this->model->add($this->params);
        } catch (Throwable $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     * @return mixed
     * 我的预约
     */
    public function my()
    {
        $this->params['member_id'] = $this->memberId;
        $reType = input('re_type', 1);
        //判断用户身份
        $identify = \app\model\admin\Member::where('member_id', $this->memberId)->value('identify');
        if ($identify == 2) {
            $where['re_id'] = \app\model\home\Assistant::where('member_id', $this->memberId)->value('id');
        } else {
            $where['member_id'] = $this->memberId;
        }
        $where['status'] = 1;
        $where['re_type'] = $reType;
        return $this->model->myBook($where, $this->params);
    }

    /**
     *
     * 同意预约
     */
    public function enable()
    {
        try {
            $this->params['member_id'] = $this->memberId;
            return $this->model->approveBook($this->params);
        } catch (\Exception $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     *
     * 拒绝预约
     */
    public function disable()
    {
        try {
            $this->params['member_id'] = $this->memberId;
            return $this->model->rejectBook($this->params);
        } catch (\Exception $ex) {
            return $this->returnException($ex);
        }
    }
}
